import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SystemDeptApi } from '#/api/system/dept';

import { z } from '#/adapter/form';
import { getDeptList } from '#/api/system/dept';
import { $t } from '#/locales';

/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'deptName',
      label: '部门名称',
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', ['部门名称', 2]))
        .max(20, $t('ui.formRules.maxLength', ['部门名称', 20])),
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: getDeptList,
        class: 'w-full',
        labelField: 'deptName',
        valueField: 'deptId',
        childrenField: 'children',
      },
      fieldName: 'parentId',
      label: '上级部门',
    },
    {
      component: 'Input',
      fieldName: 'showSort',
      label: '显示序号',
      defaultValue: 0,
    },
    {
      component: 'Input',
      fieldName: 'leaderUserId',
      label: '负责人',
      defaultValue: 0,
    },
    {
      component: 'Input',
      fieldName: 'deptPhone',
      label: '联系电话',
    },
    {
      component: 'Input',
      fieldName: 'deptEmail',
      label: '邮箱',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'deptStatus',
      label: '部门状态',
    },
  ];
}
/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<SystemDeptApi.SystemDept>,
): VxeTableGridOptions<SystemDeptApi.SystemDept>['columns'] {
  return [
    {
      align: 'left',
      field: 'deptName',
      fixed: 'left',
      title: '部门名称',
      treeNode: true,
    },
    {
      cellRender: { name: 'CellTag' },
      field: 'deptStatus',
      title: '部门状态',
    },
    {
      field: 'showSort',
      title: '显示序号',
      width: 100,
    },
    {
      field: 'deptEmail',
      title: '邮箱',
    },
    {
      field: 'deptPhone',
      title: '电话',
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: '部门名称',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'append',
            type: 'primary',
            title: '新增下级',
          },
          {
            code: 'edit',
            type: 'primary',
          },
          {
            code: 'delete',
            type: 'danger',
            disabled: (row: SystemDeptApi.SystemDept) => {
              return !!(row.children && row.children.length > 0);
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: '操作',
      width: 200,
    },
  ];
}
